FROM centos:7

ENV NODE_VERSION 4.4.2
ENV NODE_ENV development
ENV PORT 40009
ENV APP_NAME pi-collect
ENV ROOT_DIR terran-devops
ENV OSS pisns-oss-dev.oss-cn-hangzhou.aliyuncs.com

RUN yum install -y wget \
	&& wget https://nodejs.org/dist/v"$NODE_VERSION"/node-v"$NODE_VERSION"-linux-x64.tar.gz

RUN tar -xf node-v"$NODE_VERSION"-linux-x64.tar.gz \
	&& mv node-v"$NODE_VERSION"-linux-x64 /usr/local/lib \
	&& ln -s /usr/local/lib/node-v"$NODE_VERSION"-linux-x64/bin/node /usr/local/bin/ \
	&& ln -s /usr/local/lib/node-v"$NODE_VERSION"-linux-x64/bin/npm /usr/local/bin/ \
	&& npm config set registry https://registry.npm.taobao.org \
	&& npm install pm2 -g \
	&& rm -rf node-v"$NODE_VERSION"-linux-x64.tar.gz

RUN rm -rf web_app \
	&& mkdir web_app

WORKDIR /web_app

RUN touch startup.sh \
	&& echo '#!/bin/bash' >> startup.sh \
	&& echo 'rm -rf $APP_NAME.tgz && wget http://$OSS/$ROOT_DIR/pkgs/frontend/$NODE_ENV/latest/$APP_NAME.tgz &&' >> startup.sh \
	&& echo 'tar -xf $APP_NAME.tgz && rm -rf $APP_NAME.tgz &&' >> startup.sh \
	&& echo 'cd server &&' >> startup.sh \
	&& echo 'npm install --production --registry=https://registry.npm.taobao.org &&' >> startup.sh \
	&& echo 'cd .. &&' >> startup.sh \
	&& echo 'node server/server.js PORT=$PORT APP_NAME=$APP_NAME' >> startup.sh \
	&& chmod +x startup.sh

EXPOSE "$PORT"

CMD ["./startup.sh"]
